package com.xinxin.facelinker.activity;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.easemob.chatuidemo.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.xinxin.facelinker.Config;
import com.xinxin.facelinker.adapter.PalsMotionAdapter;
import com.xinxin.facelinker.domain.ShowPalsMotion;
import com.xinxin.facelinker.ui.button.views.ButtonFloat;
import com.xinxin.facelinker.ui.menu.ContextMenuDialogFragment;
import com.xinxin.facelinker.ui.menu.MenuObject;
import com.xinxin.facelinker.ui.menu.MenuParams;

import java.util.ArrayList;
import java.util.List;

/**
 * A placeholder fragment containing a simple view.
 */
public class ShowPalsMotionFragment extends Fragment {

    private String my_account_num;
    private ListView lvShowPalsMotion;
    private PalsMotionAdapter adapter;
    private HttpUtils httpUtils = new HttpUtils();
    private List<ShowPalsMotion> list=new ArrayList<ShowPalsMotion>();
    private ButtonFloat btnMenu;
    private FragmentManager fragmentManager;
    private DialogFragment mMenuDialogFragment;

    public ShowPalsMotionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_show_pals_motion, container, false);

        my_account_num = getActivity().getIntent().getStringExtra(Config.KEY_MY_ACCOUNT_NUM);
        lvShowPalsMotion = (ListView) root.findViewById(R.id.lvShowPalsMotion);
        btnMenu = (ButtonFloat) root.findViewById(R.id.btnMenu);
        fragmentManager = getFragmentManager();

        initMenuFragment();

        btnMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMenuDialogFragment.onAttach(getActivity());
                fragmentManager.beginTransaction().add(mMenuDialogFragment, "").commit();
            }
        });

        getPalsMotion();

        return root;
    }

    private void getPalsMotion(){
        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.ACTION, Config.ACTION_SHOW_PALS_MOTION);
        params.addBodyParameter(Config.KEY_MY_ACCOUNT_NUM, my_account_num);
        httpUtils.send(HttpRequest.HttpMethod.POST, Config.CATEGORIES_URL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Gson gson = new Gson();
                list = gson.fromJson(responseInfo.result, new TypeToken<List<ShowPalsMotion>>() {
                }.getType());
                adapter = new PalsMotionAdapter(getActivity(), list);
                lvShowPalsMotion.setAdapter(adapter);
                lvShowPalsMotion.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        Intent intent = new Intent(getActivity(), ShowMotionActivity.class);
                        intent.putExtra(Config.KEY_MOTION_PHOTO_URL, list.get(position).getMotion_photo_url());
                        intent.putExtra(Config.KEY_MOTION_ID, list.get(position).getMotion_id());
                        intent.putExtra(Config.KEY_MOTION, list.get(position).getMotion());
                        intent.putExtra(Config.KEY_PHOTO_URL, list.get(position).getPhoto_url());
                        intent.putExtra(Config.KEY_DATE, list.get(position).getDate());
                        intent.putExtra(Config.KEY_NICKNAME, list.get(position).getNickname());

                        startActivity(intent);
                    }
                });

//                refresh.finishRefreshing();
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(getActivity(), R.string.show_pals_motion_fail, Toast.LENGTH_SHORT).show();
//                refresh.finishRefreshing();
            }
        });
    }

    private void initMenuFragment() {
        MenuParams menuParams = new MenuParams();
        menuParams.setActionBarSize((int) getResources().getDimension(R.dimen.tool_bar_height));
        menuParams.setMenuObjects(getMenuObjects());
        menuParams.setClosableOutside(false);
        mMenuDialogFragment = ContextMenuDialogFragment.newInstance(menuParams);
    }

    private List<MenuObject> getMenuObjects() {

        List<MenuObject> menuObjects = new ArrayList<>();

        MenuObject close = new MenuObject();
        close.setResource(R.drawable.btn_close);

        MenuObject search = new MenuObject("搜索");
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_search);
        search.setBitmap(b);

        MenuObject my_motion = new MenuObject("自己动态");
        b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_my_motion);
        my_motion.setBitmap(b);

        MenuObject near_pal = new MenuObject("附近的人");
        BitmapDrawable bd = new BitmapDrawable(getResources(),
                BitmapFactory.decodeResource(getResources(), R.drawable.btn_near_pal));
        near_pal.setDrawable(bd);

        MenuObject blacklist = new MenuObject("黑名单");
        b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_blacklist);
        blacklist.setBitmap(b);

        MenuObject female_toplist = new MenuObject("女神榜");
        b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_female_toplist);
        female_toplist.setBitmap(b);

        MenuObject male_toplist = new MenuObject("男神榜");
        b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_male_toplist);
        male_toplist.setBitmap(b);

        MenuObject add_motion = new MenuObject("添加动态");
        b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_add);
        add_motion.setBitmap(b);

        menuObjects.add(close);
        menuObjects.add(search);
        menuObjects.add(my_motion);
        menuObjects.add(near_pal);
        menuObjects.add(female_toplist);
        menuObjects.add(male_toplist);
        menuObjects.add(add_motion);
        menuObjects.add(blacklist);

        return menuObjects;
    }

    public boolean onCreateOptionsMenu(final Menu menu) {
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item_image:
                if (fragmentManager.findFragmentByTag(ContextMenuDialogFragment.TAG) == null) {
                    mMenuDialogFragment.show(fragmentManager, ContextMenuDialogFragment.TAG);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onBackPressed() {
        if (mMenuDialogFragment != null && mMenuDialogFragment.isAdded()) {
            mMenuDialogFragment.dismiss();
        } else {
//            finish();
        }
    }
}
